package com.ruoyi.project.demo.arrange.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.demo.arrange.mapper.CourseArrangeMapper;
import com.ruoyi.project.demo.arrange.domain.CourseArrange;
import com.ruoyi.project.demo.arrange.service.ICourseArrangeService;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.demo.course.domain.Course;
import com.ruoyi.project.demo.course.mapper.CourseMapper;
import com.ruoyi.project.demo.order.domain.CourseOrder;
import com.ruoyi.project.demo.order.service.ICourseOrderService;

/**
 * 知识安排Service业务层处理
 *
 * @author demo
 * @date 2020-04-19
 */
@Service
public class CourseArrangeServiceImpl implements ICourseArrangeService {
    @Autowired
    private CourseArrangeMapper courseArrangeMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ICourseOrderService orderService;

    /**
     * 查询知识安排
     *
     * @param id 知识安排ID
     * @return 知识安排
     */
    @Override
    public CourseArrange selectCourseArrangeById(Long id) {
        return courseArrangeMapper.selectCourseArrangeById(id);
    }

    /**
     * 查询知识安排列表
     *
     * @param courseArrange 知识安排
     * @return 知识安排
     */
    @Override
    public List<CourseArrange> selectCourseArrangeList(CourseArrange courseArrange) {
        //先查看是否有自己的订单
        List<CourseArrange> courseArranges = courseArrangeMapper.selectCourseArrangeList(courseArrange);
        for (int i = 0; i < courseArranges.size(); i++) {
            courseArranges.get(i).setCourse(courseMapper.selectCourseById(courseArranges.get(i).getCourseId()));
            //查看订单次数
            try {
                if (courseArrange.getUserId() != null) {
                    CourseOrder order = new CourseOrder();
                    order.setUserId(courseArrange.getUserId());
                    order.setArrangeId(courseArranges.get(i).getCourse().getId());
                    List<CourseOrder> orders = orderService.selectCourseOrderList(order);
                    if (orders != null && orders.size() > 0) {
                        courseArranges.get(i).getCourse().setNum(orders.get(0).getNum());
                    }
                }
            } catch (Exception e) {

            }
            try {
                courseArranges.get(i).setCourseName(courseArranges.get(i).getCourse().getName());
            } catch (Exception e) {

            }
        }
        return courseArranges;
    }

    /**
     * 新增知识安排
     *
     * @param courseArrange 知识安排
     * @return 结果
     */
    @Override
    public int insertCourseArrange(CourseArrange courseArrange) {
        courseArrange.setCreateBy(ShiroUtils.getLoginName());
        courseArrange.setCreateTime(DateUtils.getNowDate());
        return courseArrangeMapper.insertCourseArrange(courseArrange);
    }

    /**
     * 修改知识安排
     *
     * @param courseArrange 知识安排
     * @return 结果
     */
    @Override
    public int updateCourseArrange(CourseArrange courseArrange) {
        courseArrange.setUpdateTime(DateUtils.getNowDate());
        return courseArrangeMapper.updateCourseArrange(courseArrange);
    }

    /**
     * 删除知识安排对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCourseArrangeByIds(String ids) {
        return courseArrangeMapper.deleteCourseArrangeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除知识安排信息
     *
     * @param id 知识安排ID
     * @return 结果
     */
    @Override
    public int deleteCourseArrangeById(Long id) {
        return courseArrangeMapper.deleteCourseArrangeById(id);
    }
}
